package org.prashant.sqlduniya.sqlmanager.util;


import java.io.*;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.w3c.dom.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

/**
 * This class provides helper methods to invoke XSLT
 * transformations, and manipulate their output.
 */
public class TransformUtil {

	// Global value so it can be ref'd by the tree-adapter
	static Document document;

	static String transform(StreamSource stylesource,Document doc)
	{
		String transformResult = "";
  		try {
		  // Use a Transformer for output
			TransformerFactory tFactory =  TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer(stylesource);
			StringWriter out = new StringWriter();

			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(out);

			transformer.transform(source, result);

			out.flush();
			transformResult = out.toString();

			out.close();
					}
		catch (TransformerConfigurationException tce) {
			// Error generated by the parser
			System.out.println ("\n** Transformer Factory error");
			System.out.println("   " + tce.getMessage() );

			// Use the contained exception, if any
			Throwable x = tce;

			if (tce.getException() != null)
				x = tce.getException();
		//	x.printStackTrace();

		}
		catch (TransformerException te) {
			// Error generated by the parser
			System.out.println ("\n** Transformation error");
			System.out.println("   " + te.getMessage() );

			// Use the contained exception, if any
			Throwable x = te;

			if (te.getException() != null)
				x = te.getException();
			//x.printStackTrace();

		}
		catch (IOException ioe) {
			// I/O error
			ioe.printStackTrace();
		}

		return transformResult;


	}

	/**
	 Carry out a transformation and return the result in string form.
	 @param xslstr XSL string
	 @param doc Document to be transformed
	 */
	public static String transformFromString(String xslstr,Document doc)
	{
		StringReader xslread = new StringReader(xslstr);
		StreamSource stylesource = new StreamSource(xslread);
		return transform(stylesource,doc);
	}

	/**
	 Carry out a transformation and return the result in string form.
	 @param xslstr Name of XSL file
	 @param doc Document to be transformed
	 */
	public static String transform(String xslFilename, Document doc)
	{
		File stylesheet = new File(xslFilename);
		StreamSource stylesource = new StreamSource(stylesheet);
		return transform(stylesource,doc);
	}

	/**
	 Carry out a transformation and return the result in string form.
	 @param xslFilename Name of XSL file
	 @param xmlFilename Name of XML file to be transformed
	 @validate Whether or not to validate the XML file first
	 */
	public static String transform(String xslFilename, String xmlFilename, boolean validate) {

		String transformResult = "";

		try {
			XMLValidator validator = new XMLValidator(xmlFilename, true, validate);

			document = validator.getDocument();
			transformResult =  transform(xslFilename,document);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return transformResult;

	} // main

	/**
	 Carry out a transformation and return the result in string form.
	 @param xslFilename Name of XSL file
	 @param xmlFilename Name of XML file to be transformed
	 */
	public static String transform(String xslFilename, String xmlFilename) {
		return transform(xslFilename, xmlFilename, true);
	} // main



	/**
	 When run from the command line, this method uses the stylesheet
	 passed in as the first argument to transform the xmlfile passed in
	 as the second argument.
	 */
	public static void main(String args[]) {
		File xsl = null, xml = null, dtd = null;

		try {
			if (args.length != 2) {
				System.err.println ("Usage: java TransformUtil stylesheet xmlfile");
				System.exit (1);
			}

			xsl = new File(args[0]);
			xml = new File(args[1]);

		}
		catch (Exception e) {
			e.printStackTrace(System.err);
		}

		if (!(xsl.exists()))  {
			System.err.println("stylesheet is not accessible");
		}

		if (!(xml.exists())) {
			System.err.println("xmlfile is not accessible");
		}

		System.out.println(transform(args[0], args[1]));
	}


}
